package com.yami.shop.user.platform.controller;

import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.user.common.dto.UserLevelDto;
import com.yami.shop.user.common.enums.GrowthLogSourceEnum;
import com.yami.shop.user.common.model.UserLevel;
import com.yami.shop.user.common.model.UserLevelLang;
import com.yami.shop.user.common.service.UserLevelLangService;
import com.yami.shop.user.common.service.UserLevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * 会员等级
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Tag(name = "会员等级")
@RestController
@AllArgsConstructor
@RequestMapping("/user/userLevel" )
public class UserLevelController {

    private final UserLevelService userLevelService;
    private final UserLevelLangService userLevelLangService;

    @SysLog("查看会员等级" )
    @GetMapping("/list" )
    @Operation(summary = "获取用户等级列表" , description = "获取用户等级列表")
    @PreAuthorize("@pms.hasPermission('user:userLevel:list')" )
    public ServerResponseEntity<List<UserLevelDto>> listUserLevel(Integer userLevelType) {
        return ServerResponseEntity.success(userLevelService.listUserLevelsByUserLevelType(userLevelType));
    }

    @GetMapping("/info/{id}" )
    @Operation(summary = "查询会员等级" , description = "通过id查询会员等级")
    @Parameter(name = "id", description = "会员等级id" )
    public ServerResponseEntity<UserLevel> getById(@PathVariable("id") Long id) {
        UserLevel userLevel = userLevelService.getUserLevelById(id);
        return ServerResponseEntity.success(userLevel);
    }

    @SysLog("新增会员等级" )
    @PostMapping
    @Operation(summary = "新增会员等级" , description = "新增会员等级")
    @PreAuthorize("@pms.hasPermission('user:userLevel:save')" )
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid UserLevel userLevel) {
        userLevelService.save(userLevel);
        // 保存等级国际化
        List<UserLevelLang> userLevelLangList = new ArrayList<>();
        UserLevelLang userLevelLang = new UserLevelLang();
        userLevelLang.setLevelId(userLevel.getId());
        userLevelLang.setLang(0);
        userLevelLang.setLevelName(userLevel.getLevelNameCn());
        userLevelLangList.add(userLevelLang);
        userLevelLang.setLang(1);
        userLevelLang.setLevelName(userLevel.getLevelNameEn());
        userLevelLangList.add(userLevelLang);
        userLevelLangService.saveBatch(userLevelLangList);
        return ServerResponseEntity.success();
    }

    @SysLog("修改会员等级" )
    @PutMapping
    @Operation(summary = "修改会员等级" , description = "修改会员等级")
    @PreAuthorize("@pms.hasPermission('user:userLevel:update')" )
    public ServerResponseEntity<Void> updateById(@RequestBody @Valid UserLevel userLevel) {
        userLevelService.updateUserLevelList(userLevel);
        return ServerResponseEntity.success();
    }

    @SysLog("删除会员等级" )
    @DeleteMapping
    @Operation(summary = "删除会员等级" , description = "删除会员等级")
    @PreAuthorize("@pms.hasPermission('user:userLevel:delete')" )
    public ServerResponseEntity<Boolean> removeById(@RequestBody @Valid UserLevel userLevel) {
        return ServerResponseEntity.success(userLevelService.deleteUserLevel(userLevel));
    }

    @PutMapping("/updateUserLevel")
    @Operation(summary = "更新用户会员等级" , description = "更新用户会员等级")
    public ServerResponseEntity<Boolean> updateUserLevel(@RequestBody @Valid UserLevel userLevel) {
        return ServerResponseEntity.success(userLevelService.updateUserLevel(userLevel));
    }

    @PutMapping("/updateGrowth")
    @Operation(summary = "平台批量修改会员成长值" , description = "平台批量修改会员成长值")
    @PreAuthorize("@pms.hasPermission('user:userLevel:updateGrowth')" )
    public ServerResponseEntity<Boolean> batchUserGrowth(@RequestBody UserUpdateParam param) {
        param.setGrowthSource(GrowthLogSourceEnum.SYSTEM.value());
        return ServerResponseEntity.success(userLevelService.batchUpdateGrowth(param));
    }

    @PutMapping("/updateScore")
    @Operation(summary = "平台批量修改会员积分" , description = "平台批量修改会员积分")
    @PreAuthorize("@pms.hasPermission('user:userLevel:updateScore')" )
    public ServerResponseEntity<Boolean> batchUserScore(@RequestBody UserUpdateParam param) {
        return ServerResponseEntity.success(userLevelService.batchUpdateScore(param));
    }


}
